package com.lzxuni.modules.plantingprogram.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.basics.entity.BasicTree;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.cropinformation.entity.TblCropInformation;
import com.lzxuni.modules.cropinformation.entity.TblPlantingInformation;
import com.lzxuni.modules.cropinformation.service.TblCropInformationService;
import com.lzxuni.modules.cropinformation.service.TblPlantingInformationService;
import com.lzxuni.modules.plantingprogram.entity.PlantingProgramDetail;
import com.lzxuni.modules.plantingprogram.entity.PlantingProgramUse;
import com.lzxuni.modules.plantingprogram.service.PlantingProgramDetailService;
import com.lzxuni.modules.plantingprogram.service.PlantingProgramUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 种植方案_使用 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-12-18
 */
@RestController
@RequestMapping("/PlantingProgramUse")
public class PlantingProgramUseController extends BaseController {

    @Autowired
    private PlantingProgramUseService plantingProgramUseService;
    @Autowired
    private TblPlantingInformationService plantingInformationService;
    @Autowired
    private TblCropInformationService tblCropInformationService;
    @Autowired
    private PlantingProgramDetailService plantingProgramDetailService;

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/PlantingProgram/use/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        PlantingProgramUse plantingProgramUse = JSON.parseObject(queryJson, PlantingProgramUse.class);
        PageData pageData = getPageData(plantingProgramUseService.queryPage(pageParameter, plantingProgramUse));
        return R.ok().put("data", pageData);
    }

    //表单选择方案页面页
    @RequestMapping("/Form")
    public ModelAndView Form(String keyValue, String queryJson) {
        ModelAndView mv = new ModelAndView("/PlantingProgram/use/form");
        BasicTree basicTree = JSON.parseObject(queryJson, BasicTree.class);
        String treeId = basicTree.getTreeId();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
        Date asd = new Date();
        String shijian = sdf1.format(asd);
        TblPlantingInformation plantInfo = plantingInformationService.getOne(new QueryWrapper<TblPlantingInformation>().eq("land_id",treeId).eq("planting_year",shijian));
        if(plantInfo != null){
            TblCropInformation cropIfo = tblCropInformationService.getById(plantInfo.getCropModela());
            mv.addObject("cropIfo", cropIfo);
        }else{
            TblCropInformation cropIfoe = new TblCropInformation();
            mv.addObject("cropIfo", "cropIfoe");
        }
        mv.addObject("basicTree", basicTree);
        return mv;
    }
    //表单反馈页
    @RequestMapping("/FormEdit")
    public ModelAndView FormEdit(String keyValue, String queryJson) {
        ModelAndView mv = new ModelAndView("/PlantingProgram/use/formfk");
        BasicTree basicTree = JSON.parseObject(queryJson, BasicTree.class);
        PlantingProgramUse plantProUse = plantingProgramUseService.queryFanKui(keyValue);
        List<PlantingProgramDetail> plantProDeList=plantingProgramDetailService.list(new QueryWrapper<PlantingProgramDetail>().eq("program_id",plantProUse.getProgramId()).orderByDesc("create_date"));
        mv.addObject("plantProUse", plantProUse);
        mv.addObject("basicTree", basicTree);
        mv.addObject("plantProDeList", plantProDeList);
        return mv;
    }
    //表单反馈页
    @RequestMapping("/See")
    public ModelAndView See(String keyValue, String queryJson) {
        ModelAndView mv = new ModelAndView("/PlantingProgram/use/see");
        BasicTree basicTree = JSON.parseObject(queryJson, BasicTree.class);
        PlantingProgramUse plantProUse = plantingProgramUseService.queryFanKui(keyValue);
        List<PlantingProgramDetail> plantProDeList=plantingProgramDetailService.list(new QueryWrapper<PlantingProgramDetail>().eq("program_id",plantProUse.getProgramId()).orderByDesc("create_date"));
        mv.addObject("plantProUse", plantProUse);
        mv.addObject("basicTree", basicTree);
        mv.addObject("plantProDeList", plantProDeList);
        return mv;
    }
    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue,PlantingProgramUse plantingProgramUse) {
        plantingProgramUse.setUseId(keyValue);
        if(StringUtils.isEmpty(plantingProgramUse.getUseId())){
            String useId = UuidUtil.get32UUID();
            plantingProgramUse.setUseId(useId);
            plantingProgramUse.setCreateTime(new Date());
             plantingProgramUseService.save(plantingProgramUse);
             return R.ok("保存成功");
        }else{
            plantingProgramUse.setFeedbackDate(plantingProgramUse.getFeedbackDate());
            plantingProgramUse.setFeedbackContent(plantingProgramUse.getFeedbackContent());
            plantingProgramUseService.updateById(plantingProgramUse);
            return R.ok("修改成功");
        }
    }

    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        plantingProgramUseService.removeById(keyValue);
        return R.ok("删除成功");
    }

//    方案选择下拉框
    @RequestMapping("/GetTreeFa")
    public Object getTree(String landId) {
        List<Tree> puseList = plantingProgramUseService.queryFangAn(landId);
        return R.ok().put("data", puseList);
    }


}

